$(document).ready(function() {

	$('.select2').select2();
	$('#reservation').daterangepicker({
		startDate: '2018-11-01',
		locale: {
			format: 'YYYY-MM-DD'
		}
	});

	var searchKind = 4;
	getAllData(1);//初始化请求

	//判断搜索类型   1：快捷搜索。 2：月份。 3： 日期。
	$('.fast_sel').change(function() {
		searchKind = 1;
	})
	$('.month_sel').change(function() {
		searchKind = 2;
	})
	$('.date_pick').change(function() {
		searchKind = 3;
	})

	function getAllData(curr) {

		var Time = $('.fast_sel').select2('val'); //日期快捷搜索
		var Month = $('.month_sel').select2('val'); //月份搜索
		var data_rang = $('#reservation').val(); //时间范围
		//时间范围转化格式
		var start = data_rang.split(' -')[0];
		var end = data_rang.split('- ')[1];

		$.ajax({
			type: "get",
			url: "https://www.retux.cn/MgDataLog",
			async: true,
			data: {
				Kind: searchKind,
				Time: Time,
				Month: Month,
				start: start,
				end: end,
				page: curr,
				row: 20
			},
			success: function(data) {
				if(data[0].length == 0) {
					layer.msg('暂无数据');
				}
				dataView(data[0]);
				pageDivide(curr, data[1]);
				$('.all_money').text(data[2]);
				$('.all_increase').text(data[3])
			}
		});
	}

	function dataView(data) {
		$('.table_data').html("")
		for(var i = 0; i < data.length; i++) {
			var Tr = `
		 			    <tr>i
	      					<td>${data[i][0]}</td>
	      					<td>${data[i][1]}</td>
	      					<td>${data[i][2]}</td>
	      					<td>${data[i][3]}</td>
	      					<td>${data[i][4]}</td>
	      					<td>${data[i][5]}</td>
	      					<td>${data[i][6]}</td>
	      					<td>${data[i][7]}</td>
	      					<td>${data[i][8]}</td>
	      					<td>${data[i][9]}</td>
	      					<td>${data[i][10]}</td>
	      				</tr>
		 			`;

			$('.table_data').append(Tr)
		}
	}
	
	//搜索
	$('.submit').click(function() {
		
		getAllData(1);
	})

	function pageDivide(curr, page_num) {
		layui.use(['laypage', 'layer'], function() {
			var laypage = layui.laypage,
				layer = layui.layer;
			laypage.render({
				elem: 'demo7',
				count: page_num,
				limit: 20,
				curr: curr,
				jump: function(obj, first) {
					if(!first) {
						getAllData(obj.curr);

					}
				}
			});
		})
	}

})